﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;

namespace Directory.Web
{
	public partial class Admin_LogoView : System.Web.UI.Page, ICallbackEventHandler
	{
		protected string currentPath = string.Empty;
		protected string parentPath = string.Empty;
		protected string logoBasePath = ConfigurationManager.AppSettings["LogoBasePath"].Replace("~/", "/");

		protected void Page_Load(object sender, EventArgs e)
		{
			#region pageload
			string absolutePath = Server.MapPath(GetFullPath());

			DirectoryInfo dirInfo = new DirectoryInfo(absolutePath);
			if (dirInfo.Exists)
			{
				DirectoryInfo[] folderList = dirInfo.GetDirectories();
				FileInfo[] fileList = dirInfo.GetFiles();
				if (folderList.Length + fileList.Length > 0)
				{
					rptFolders.DataSource = folderList;
					rptFiles.DataSource = fileList;

					rptFolders.DataBind();
					rptFiles.DataBind();
				}
			}
			#endregion

			#region AjaxCall
			string jsBlock = Page.ClientScript.GetCallbackEventReference(this, "filename", "deletecallback", "context", "deleteerrorcalback", false);
			string jsfunc = @"function ajaxdeletefile(filename,context){" + jsBlock + "}";
			Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ajaxdeletefile", jsfunc, true);
			#endregion
		}

		//新建文件夹
		protected void btnCreateFolder_Click(object sender, EventArgs e)
		{
			string absolutePath = Server.MapPath(GetFullPath());

			if (string.IsNullOrEmpty(Request.Form["folderName"]))
			{
				return;
			}

			DirectoryInfo dirInfo = new DirectoryInfo(absolutePath + "/" + Request.Form["folderName"]);
			if (!dirInfo.Exists)
			{
				try
				{
					dirInfo.Create();
					Page.ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('创建成功！');</script>");
					Response.Redirect(Request.RawUrl);
				}
				catch (Exception ex)
				{
					Page.ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('创建失败！');</script>");
					Response.Write(ex);
					return;
				}
			}
			else
			{
				Page.ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>alert('文件夹已存在！');</script>");
				return;
			}
		}

		private string GetFullPath()
		{
			string basePath = string.Empty;
			try
			{
				basePath = ConfigurationManager.AppSettings["LogoBasePath"].Trim();
			}
			catch
			{
				try
				{
					basePath = Session["LogoBasePath"].ToString().Trim();
				}
				catch
				{
					try
					{
						basePath = Server.HtmlDecode(Request.QueryString["LogoBasePath"].Trim());
					}
					catch { }
				}
			}

			try
			{
				currentPath = Server.HtmlDecode(Request.QueryString["CurrentPath"].Trim());
			}
			catch
			{
				try
				{
					currentPath = Session["CurrentPath"].ToString().Trim();
				}
				catch { }
			}

			if (!basePath.EndsWith("/") && !string.IsNullOrEmpty(basePath))
			{
				basePath += "/";
			}

			string fullPath = basePath + currentPath;
			while (fullPath.IndexOf("//") > 0)
			{
				fullPath = fullPath.Replace("//", "/");
			}
			if (currentPath.IndexOf("/") >= 0)
			{
				parentPath = currentPath.Remove(currentPath.LastIndexOf("/"));
			}
			return fullPath;
		}

		#region ICallbackEventHandler 成员

		private string ajaxResult = string.Empty;

		public string GetCallbackResult()
		{
			return ajaxResult;
		}

		public void RaiseCallbackEvent(string eventArgument)
		{
			if (string.IsNullOrEmpty(eventArgument))
			{
				ajaxResult = "文件名或目录为空！";
				return;
			}
			string absolutePath = Server.MapPath(GetFullPath());
			FileInfo file = new FileInfo(absolutePath + "/" + eventArgument);
			if (file.Exists)
			{
				try
				{
					file.Delete();
					ajaxResult = eventArgument + "删除成功！";
				}
				catch
				{
					ajaxResult = "删除失败！";
				}
			}
			else
			{
				DirectoryInfo dir = new DirectoryInfo(absolutePath + "/" + eventArgument);
				if (dir.Exists)
				{
					if (dir.GetFiles().Length == 0 && dir.GetDirectories().Length == 0)
					{
						try
						{
							dir.Delete();
							ajaxResult = "目录" + eventArgument + "删除成功！";
						}
						catch
						{
							ajaxResult = "删除失败！";
						}
					}
					else
					{
						ajaxResult = eventArgument + "目录不为空！";//目录非空，不进行删除操作
					}
				}
				else
				{
					ajaxResult = "文件不存在！";//文件不存在
				}
			}
		}

		#endregion
	}
}
